//
//  MyTabBarViewController.m
//  HeXun
//
//  Created by ibokan on 15/11/16.
//  Copyright © 2015年 LGC. All rights reserved.
//

#import "MyTabBarViewController.h"
#import "NavigationItemToTabarControllerItemDelegate.h"
#import "Reachability.h"
@interface MyTabBarViewController ()
{
    //检查网络状态
    Reachability* _reachability;
}
@end

@implementation MyTabBarViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBarController.navigationItem.leftBarButtonItems = self.navigationItem.leftBarButtonItems;
    self.tabBarController.navigationItem.titleView = self.navigationItem.titleView;
    
    self.delegate = self.viewControllers.firstObject;
    
    _reachability = [Reachability reachabilityForInternetConnection];
    //随时监听网络状态
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    [_reachability startNotifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSLog(@"%@",item.title);
    if ([item.title isEqualToString:@"新闻"])
    {
        self.delegate = self.viewControllers[0];
    }
    if ([item.title isEqualToString:@"图片"])
    {
        self.delegate = self.viewControllers[1];
    }
    if ([item.title isEqualToString:@"行情"])
    {
        self.delegate = self.viewControllers[2];
    }
    if ([item.title isEqualToString:@"自选"]) {
        self.delegate = self.viewControllers[3];
    }
    if ([item.title isEqualToString:@"我的"]) {
        self.delegate = self.viewControllers[4];
    }
    
    [self navigationItemToTabarControllerItem];
}

- (void)navigationItemToTabarControllerItem
{
    [self.delegate navigationItemToTabarControllerItem];
}

- (void)reachabilityChanged:(NSNotification* )notification
{
    Reachability* rb = notification.object;
    UIAlertView* alertView = [UIAlertView new];
    
    switch (rb.currentReachabilityStatus)
    {
        case NotReachable:
        {
            alertView.title = @"当前无网络";
            [alertView addButtonWithTitle:@"确定"];
            [alertView show];
        }
            break;
        case ReachableViaWiFi:
        {
            NSLog(@"网络状态:wifi");
        }
            break;
        case ReachableViaWWAN:
        {
            NSLog(@"网络状态：3G");
        }
            break;
            
        default:
            break;
    }
    
}

-(void)dealloc
{
    [_reachability stopNotifier];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:kReachabilityChangedNotification object:nil];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
